/**
 * Copyright (c) 2011-2023, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jse.tpl;

import java.io.File;

import com.jse.Fs;
import com.jse.Lang;
import com.jse.Strings;

/**
 * StringSource 用于从 String 变量中加载模板内容
 */
public class ISource {
	
	private String cacheKey;
	private StringBuilder content;
	private String id=Lang.uuid();
	
	/**
	 * 构造 StringSource
	 * @param content 模板内容
	 * @param cache true 则缓存 Template，否则不缓存
	 */
	public ISource(String content, boolean cache) {
		this(content, cache ? Lang.md5(content) : null);
	}
	
	public ISource(File file, boolean cache) {
		if (!file.exists()) {
			throw new IllegalArgumentException("content can not be blank");
		}
		this.id=file.toString();
		this.content = new StringBuilder(Fs.readString(file.toPath()));
		this.cacheKey = cache ? Lang.md5(file) : null;
	}
	
	/**
	 * 构造 StringSource
	 * @param content 模板内容
	 * @param cacheKey 缓存 Template 使用的 key，值为 null 时不缓存
	 */
	public ISource(String content, String cacheKey) {
		if (Strings.isBlank(content)) {
			throw new IllegalArgumentException("content can not be blank");
		}
		this.content = new StringBuilder(content);
		this.cacheKey = cacheKey;
	}
	public ISource(File file, String cacheKey) {
		if (!file.exists()) {
			throw new IllegalArgumentException("content can not be blank");
		}
		this.id=file.toString();
		this.content = new StringBuilder(Fs.readString(file.toPath()));
		this.cacheKey = cacheKey;
	}
	
	public ISource(StringBuilder content, boolean cache) {
		this(content, cache && content != null ? Lang.md5(content.toString()) : null);
	}
	
	public ISource(StringBuilder content, String cacheKey) {
		if (content == null || content.length() == 0) {
			throw new IllegalArgumentException("content can not be blank");
		}
		this.content = content;
		this.cacheKey = cacheKey;			// cacheKey 值为 null 时不缓存
	}
	
	public boolean isModified() {
		return false;
	}
	
	public String getCacheKey() {
		return cacheKey;
	}
	
	public StringBuilder getContent() {
		return content;
	}
	
	public String getId() {
		return id;
	};
	
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("cacheKey : ").append(cacheKey).append("\n");
		sb.append("content : ").append(content).append("\n");
		return sb.toString();
	}
}







